home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / csrc1.arc / COOTXT.C < prev    next >
C/C++ Source or Header  |  1989-07-27  |  2KB  |  110 lines

  1. /*
  2.  * cootxt
  3.  */
  4.  
  5. /*)BUILD    $(TKBOPTIONS) = {
  6.             TASK    = ...CTX
  7.         }
  8. */
  9.  
  10. #ifdef    DOCUMENTATION
  11.  
  12. title    cootxt    Cookie text file concatenator
  13. index        Cookie text file concatenator
  14.  
  15. synopsis
  16.  
  17.     cootxt [name ...]
  18.  
  19. description
  20.  
  21.     Concatenate each file, making sure that the last
  22.     cookie of each file is terminated by '%%' and
  23.     that there are no null-cookies.
  24.     
  25.     cootxt accepts wild-card file name arguments; if no
  26.     files are given, it copies stdin.
  27.  
  28. diagnostics
  29.  
  30.     .lm +8
  31.     .s.i -8;"file name": cannot open
  32.     .s.i -8;"file name": illegal file name
  33.     .lm -8
  34.  
  35. author
  36.  
  37.     Martin Minow
  38.  
  39. bugs
  40.  
  41. #endif
  42.  
  43. #include <stdio.h>
  44.  
  45. char    line[513];
  46.  
  47. main(argc, argv)
  48. int        argc;
  49. char        *argv[];
  50. {
  51.     register int    i, nfiles;
  52.     register FILE    *fp;
  53.     int        gotcha;
  54.  
  55.     nfiles = 0;
  56.     if (argc < 2) {
  57.         ++nfiles;
  58.         process(stdin);
  59.     }
  60.     else {
  61. #ifdef unix
  62.         for (i = 1; i < argc; ++i) {
  63.         if ((fp = fopen(argv[i], "r")) == NULL) {
  64.             perror(argv[i]);
  65.         }
  66.         else {
  67.             ++nfiles;
  68.             process(fp);
  69.             fclose(fp);
  70.         }
  71.         }
  72. #else
  73.         for (i = 1; i < argc; ++i) {
  74.         if ((fp = fwild(argv[i], "r")) == NULL) {
  75.             perror(argv[i]);
  76.         }
  77.         else {
  78.             for (gotcha = 0; fnext(fp) != NULL; gotcha++) {
  79.             ++nfiles;
  80.             process(fp);
  81.             }
  82.             if (gotcha == 0) {
  83.             fprintf(stderr, "\"%s\": no matching files\n",
  84.                 argv[i]);
  85.             }
  86.         }
  87.         }
  88. #endif
  89.     }
  90. }
  91.  
  92. process(fp)
  93. FILE        *fp;            /* File pointer            */
  94. {
  95.     register int    percent;
  96.     register int    flag;
  97.  
  98.     percent = FALSE;
  99.     while (fgets(line, sizeof line, fp) != NULL) {
  100.         flag = (line[0] == '%' && line[1] == '%');
  101.         if (flag && percent)
  102.         continue;
  103.         fputs(line, stdout);
  104.         percent = flag;
  105.     }
  106.     if (!percent)
  107.         fputs("%%\n", stdout);
  108. }
  109.  
  110.